Algorithm
Problem Name:
In this HackerRank Functions in Java programming problem solution,
Two strings, a and b,are called anagrams if they contain all the same characters in the same frequencies. For this challenge, the test is not case-sensitive. For example, the anagrams of CAT are CAT, ACT, tac, TCA, aTC, and CtA.
Function Description
Complete the isAnagram function in the editor.
isAnagram has the following parameters:
- string a: the first string
- string b: the second string
Returns
- boolean: If a and b are case-insensitive anagrams, return true. Otherwise, return false.
Input Format
The first line contains a string a.
The second line contains a string b.
Constraints
- 1 <= length(a),length(b) <= 50
- Strings a and b consist of English alphabetic characters.
- The comparison should NOT be case sensitive
Sample Input 0
anagram
margana
Sample Output 0
Anagrams
Explanation 0
| Character | Frequency: anagram | Frequency: margana | 
|---|---|---|
| Aora | 3 | 3 | 
| Gorg | 1 | 1 | 
| Norn | 1 | 1 | 
| Morm | 1 | 1 | 
| Rorr | 1 | 1 | 
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".
Sample Input 1
anagramm
marganaa
Sample Output 1
Not Anagrams
Explanation 1
| Character | Frequency: anagramm | Frequency: marganaa | 
|---|---|---|
| Aora | 3 | 4 | 
| Gorg | 1 | 1 | 
| Norn | 1 | 1 | 
| Morm | 2 | 1 | 
| Rorr | 1 | 1 | 
The two strings don't contain the same number of a's and m's, so we print "Not Anagrams".
Sample Input 2
Hello
hello
Sample Output 2
Anagrams
Explanation 2
| Character | Frequency: Hello | Frequency: hello | 
|---|---|---|
| Eore | 1 | 1 | 
| Horh | 1 | 1 | 
| Lorl | 2 | 2 | 
| Ooro | 1 | 1 | 
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".
Code Examples
#1 Code Example with Java Programming
Code -
                                                        Java Programming
import java.util.Scanner;
import java.util.Arrays;
public class Solution{
	static boolean isAnagram(String a, String b){
        if(a.length() != b.length())
            return false;
        else{
            char[] A = a.toLowerCase().toCharArray();
            char[] B = b.toLowerCase().toCharArray();
            
            Arrays.sort(A);
            Arrays.sort(B);
            return Arrays.equals(A, B);
        }
    }
	public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String a = scan.next();
        String b = scan.next();
        scan.close();
        boolean ret = isAnagram(a, b);
        System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
    }
}
